﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class NhaCungCapDAO
    {
        public static List<NhaCungCapDTO> SelectAll()
        {
            List<NhaCungCapDTO> list = new List<NhaCungCapDTO>();
            String strSql = "select * from NhaCungCap";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    NhaCungCapDTO ncc = new NhaCungCapDTO();
                    ncc.MaNCC = int.Parse(dr["MaNCC"].ToString());
                    ncc.TenNCC = dr["TenNCC"].ToString();
                    ncc.DiaChi = dr["DiaChi"].ToString();
                    ncc.SoDienThoai = dr["SoDienThoai"].ToString();
                    list.Add(ncc);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static List<NhaCungCapDTO> Search_by_TenNCC(string TenNCC)
        {
            List<NhaCungCapDTO> list = new List<NhaCungCapDTO>();
            String strSql = "select * from NhaCungCap where TenNCC like '%" + TenNCC + "%'";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    NhaCungCapDTO ncc = new NhaCungCapDTO();
                    ncc.MaNCC = int.Parse(dr["MaNCC"].ToString());
                    ncc.TenNCC = dr["TenNCC"].ToString();
                    ncc.DiaChi = dr["DiaChi"].ToString();
                    ncc.SoDienThoai = dr["SoDienThoai"].ToString();
                    list.Add(ncc);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static bool Insert(NhaCungCapDTO ncc)
        {
            bool result = false;

            string strSQLEmployee = "Insert into NhaCungCap(TenNCC,DiaChi,SoDienThoai) " +
                "values (@Ten,@DiaChi,@SDT)";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@Ten", ncc.TenNCC));
                sqlParams.Add(new SqlParameter("@DiaChi", ncc.DiaChi));
                sqlParams.Add(new SqlParameter("@SDT", ncc.SoDienThoai));
                // Call execute none query

                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Delete(int MaNCC)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM NhaCungCap WHERE MaNCC=@ID";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", MaNCC));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Update(NhaCungCapDTO ncc)
        {
            bool result = false;

            string strSQLEmployee = "Update NhaCungCap set TenNCC=@Ten, DiaChi=@DiaChi, SoDienThoai=@SDT " +
                "Where MaNCC=@ID";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", ncc.MaNCC));
                sqlParams.Add(new SqlParameter("@Ten", ncc.TenNCC));
                sqlParams.Add(new SqlParameter("@DiaChi", ncc.DiaChi));
                sqlParams.Add(new SqlParameter("@SDT", ncc.SoDienThoai));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
